package com.shsoft.component;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


import com.shsoft.colorfulcc.MainActivity;
import com.shsoft.common.Constant;
import com.shsoft.utils.MessageUtil;

import java.util.HashMap;

/**
 * 灯珠显示视图
 */
public class LightView extends View {
    private MainActivity activity;

    public LightView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.activity = (MainActivity) context;
    }

    //正方形显示区域的边长
    float sideLength;
    //灯珠的半径
    float radius;
    //灯珠围绕的圆形的半径
    float ledRadius;
    //圆心的坐标
    float circleCenterX,circleCenterY;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if(w > h){ //宽屏
            sideLength = h;
        }else { //长屏
            sideLength = w;
        }
        circleCenterX = w / 2f;
        circleCenterY = h / 2f;
        radius = sideLength / 2f;
        ledRadius = radius / 9f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //线条画笔
        Paint borderPaint =new Paint();//画框的笔
        borderPaint.setStyle(Paint.Style.STROKE); //设置填充样式
        borderPaint.setStrokeWidth(8); //设置画笔宽度
        //灯珠画笔
        Paint lightPaint = new Paint(); // 画灯的笔
        lightPaint.setStyle(Paint.Style.FILL); //设置填充样式
        lightPaint.setShadowLayer(5f, 3, 6, Color.GRAY);
        //绘制20展圆形灯
        canvas.save();
        if(activity.isPlaying){ // 目前是预览模式,每个灯珠都要绘制成实心
            for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                if(i != 0){ // 第一个灯珠不需要旋转画布
                    canvas.rotate( 18, circleCenterX, circleCenterY);
                }
                lightPaint.setARGB(activity.alpha, activity.lights_4display.get(i)[0], activity.lights_4display.get(i)[1], activity.lights_4display.get(i)[2]);
                canvas.drawCircle(circleCenterX,ledRadius + 3,ledRadius - 3,lightPaint);
            }
        }else { // 设置颜色模式，只有非选中的要实心
            for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                if(i != 0){ // 第一个灯珠不需要旋转画布
                    canvas.rotate( 18, circleCenterX, circleCenterY);
                }
                lightPaint.setARGB(activity.alpha, activity.lights_4display.get(i)[0], activity.lights_4display.get(i)[1], activity.lights_4display.get(i)[2]);
                borderPaint.setARGB(activity.alpha, activity.lights_4display.get(i)[0], activity.lights_4display.get(i)[1], activity.lights_4display.get(i)[2]);
                if(activity.choosedLights.get(i) == null){ // 没有被选中的状态
                    canvas.drawCircle(circleCenterX,ledRadius + 3,ledRadius - 3,lightPaint);
                }else {
                    canvas.drawCircle(circleCenterX,ledRadius + 3,ledRadius - 3,borderPaint);
                }
            }
        }
        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 计算点击位置到圆心的距离
        float distance = getDistance(event.getX(), event.getY());
        // 先根据到圆心的距离判断有没有点击灯珠
        if(distance <= (radius + 10) && distance > (radius - ledRadius * 2 - 10)){
//            MessageUtil.logDebug("点击了灯珠");
        }else{
            return super.onTouchEvent(event);
        }
        if(activity.colorMode == Constant.COLOR_MODE_SINGLE){ // 纯色模式，提示用户不用点击灯珠，预设颜色就是亮灯的颜色
            MessageUtil.toastShort(activity, "纯色模式下，预设颜色就是灯珠的显示色");
        }else {
            // 再根据角度判断点击的是哪个灯珠
            int degree = getDegree(circleCenterX, circleCenterY, circleCenterX, circleCenterY - radius, event.getX(), event.getY());
//        MessageUtil.logDebug("角度:" + degree);
            doChooseByDegree(degree);
        }
        // 重新绘制
        invalidate();
        return super.onTouchEvent(event);
    }

    // 根据角度判断是哪个被点击了，如果没有选中，则选中，如果已经选中，则取消选中
    private void doChooseByDegree(int degree){
        Integer i;
        if(degree >= 0){
            if(degree <= 9){
                i = 0;
            }else if(degree <= 27){
                i = 1;
            }else if(degree <= 45){
                i = 2;
            }else if(degree <= 63){
                i = 3;
            }else if(degree <= 81){
                i = 4;
            }else if(degree <= 99){
                i = 5;
            }else if(degree <= 117){
                i = 6;
            }else if(degree <= 135){
                i = 7;
            }else if(degree <= 153){
                i = 8;
            }else if(degree <= 171){
                i = 9;
            }else{
                i = 10;
            }
        }else {
            if(degree >= -9){
                i = 0;
            }else if(degree >= -27){
                i = 19;
            }else if(degree >= -45){
                i = 18;
            }else if(degree >= -63){
                i = 17;
            }else if(degree >= -81){
                i = 16;
            }else if(degree >= -99){
                i = 15;
            }else if(degree >= -117){
                i = 14;
            }else if(degree >= -135){
                i = 13;
            }else if(degree >= -153){
                i = 12;
            }else if(degree >= -171){
                i = 11;
            }else {
                i = 10;
            }
        }
//        MessageUtil.logDebug("点击了第" + i + "个灯珠");
        //判断该灯珠是否被选中
        if(activity.choosedLights.get(i) == null){
            activity.choosedLights.put(i,i);
        }else {
            activity.choosedLights.remove(i);
        }
    }

    // 获取某个坐标距离圆心的距离
    private float getDistance(float x, float y){
        float distanceX = circleCenterX - x;
        float distanceY = circleCenterY - y;
        return (float) Math.sqrt(distanceX * distanceX + distanceY * distanceY);
    }

    // 计算某点与平行线的夹角
    private int getDegree(float vertexPointX, float vertexPointY, float point0X, float point0Y, float point1X, float point1Y) {
        // 计算是顺时针还是逆时针
        float direction = (point0X - vertexPointX) * (point1Y - vertexPointY) - (point1X - vertexPointX) * (point0Y - vertexPointY);
        //向量的点乘
        float vector = (point0X - vertexPointX) * (point1X - vertexPointX) + (point0Y - vertexPointY) * (point1Y - vertexPointY);
        //向量的模乘
        double sqrt = Math.sqrt(
                (Math.abs((point0X - vertexPointX) * (point0X - vertexPointX)) + Math.abs((point0Y - vertexPointY) * (point0Y - vertexPointY)))
                        * (Math.abs((point1X - vertexPointX) * (point1X - vertexPointX)) + Math.abs((point1Y - vertexPointY) * (point1Y - vertexPointY)))
        );
        //反余弦计算弧度
        double radian = Math.acos(vector / sqrt);
        //弧度转角度制
        int degree = (int) (180 * radian / Math.PI);
        if(direction >= 0){ // 顺时针
            return degree;
        }else { // 逆时针
            return -degree;
        }
    }
}
